<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>操作表单元素</title>
    <style>
        .box {
            width: 400px;
            border-bottom: 1px solid red;
            margin: 100px auto;
            position: relative;
        }
        .box input {
            display: inline-block;
            width: 350px;
            height: 30px;
            border: 0;
            outline: none;
        }
        .box label {
            display: inline-block;
            position: absolute;
            right: 1px;
            bottom: 2px;
        }
    </style>
</head>
<body>
    <div class="box">
        <label for="">
            <button id="eye">查看</button>
        </label>
        <input type="password" name="" id="pwd">
    </div>
    <script>
        // 1.获取元素
        var eye = document.getElementById('eye')
        var pwd = document.getElementById('pwd')
        // 2.注册事件
        var flag = 0
        eye.onclick = function () { 
            if (flag == 0){
                pwd.type = 'text';
                eye.innerText = '加密'
                flag = 1
            } else {
                pwd.type = 'password';
                eye.innerText = '查看'
                flag = 0
            }
         }
    </script>
</body>
</html>